const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const {
  autoUpdater
} = require("electron-updater");
const {
  dialog
} = require('electron')
const log = require('electron-log');

const path = require('path')
const url = require('url')
let mainWindow
function createWindow () {
  mainWindow = new BrowserWindow({width: 1220, height: 800,icon: path.join(__dirname, './html/images/icon.ico')})
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, './html/index.html'),
    protocol: 'file:',
    slashes: true
  }))


  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()  
  }
})
/*********************************************************程序更新 ******************************************************************************************/
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');


autoUpdater.on('download-progress', (progressObj) => {
    let log_message = "Download speed: " + progressObj.bytesPerSecond;
    log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
    log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
})

autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
    const dialogOpts = {
        type: 'info',
        buttons: ['立刻更新', '稍后再说'],
        title: '软件更新',
        message: process.platform === 'win32' ? releaseNotes : releaseName,
        detail: '机械手需要更新，请重新启动软件。'
    }

    dialog.showMessageBox(dialogOpts, (response) => {
        if (response === 0) autoUpdater.quitAndInstall()
    })
})
autoUpdater.checkForUpdates();
